<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Plugin;
use addons\Builder\facade\Builder;
use app\common\tool\Result;
use think\Response;

class Addons extends Base
{

    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '插件名称', 'width' => 200])
            ->addColumn(['field' => 'type', 'title' => '插件类型', 'width' => 100, 'templet' => '<span>{{d.type == 1?"扩展插件":"集成插件"}}</span>'])
            ->addColumn(['field' => 'description', 'title' => '插件说明'])
            ->addColumn(['field' => 'installedVersion', 'title' => '已安装版本', 'width' => 120])
            ->addColumn(['field' => 'version', 'title' => '最新版本', 'width' => 120])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间', 'width' => 150])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#addonsDemo', 'width' => 200])
            ->addRightButtons([
                'install'   => [
                    'title' => '安装',
                    'url'   => url('addons/install')->build(),
                    'event' => '_submit'
                ], 'lock'   => [
                    'title' => '锁定',
                    'url'   => url('addons/locking')->build(),
                    'event' => '_submit',
                    'class' => 'danger2'
                ], 'delete' => ['title' => '卸载', 'url' => url('addons/uninstall')->build()]])
            ->addExtraHtml(['addons'])
            ->addSearch([
                ['type', '插件类型', 'select', [1 => '扩展插件', 2 => '集成插件']]
            ], false)
            ->fetch();
    }

    public function getList(): Response
    {
        return Result::fastReturn((new Plugin())->getList());
    }

    public function install(): Response
    {
        return Result::fastReturn((new Plugin())->install($this->request->post()));
    }

    public function uninstall(): Response
    {
        return Result::fastReturn((new Plugin())->uninstall($this->request->post()));
    }

    public function locking(): Response
    {
        return Result::fastReturn((new Plugin())->locking($this->request->post()));
    }
}
